﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace yysAuxiliary.FS
{
    /// <summary>
    /// 结界突破
    /// </summary>
    public class JJTP : BaseFs
    {
        protected override string prefix => "结界突破_";

        public JJTP(Action<string> SendMsg) : base(SendMsg)
        {
        }

        /// <summary>
        /// 去结界突破
        /// </summary>
        /// <returns></returns>
        private async Task GoJJTP()
        {
            await Task.Delay(1000);
            var result = OperationPoint.WhileCheck(10, 1000, OperationPoint.OperationType.JJTP, 5);
            if (!result)
            {
                SendMsg("未找到结界突破按钮，退出任务");
                return;
            }
            await Execute();
        }

        public override async Task Run()
        {
            await GetValidGameInterface();
            switch (Data.IndexGameInterface)
            {
                case Data.GameInterface.Main:
                    if (await GoTS())
                    {
                        await GoJJTP();
                    }
                    else
                    {
                        SendMsg("无法去探索界面，退出任务");
                    }
                    break;
                case Data.GameInterface.TS:
                    await GoJJTP();
                    break;
                default:
                    SendMsg("不支持的界面操作，退出任务");
                    break;
            }
            GetNextFS();
        }


        /// <summary>
        /// 循环执行
        /// </summary>
        private async Task Execute()
        {
            int i = 0;
            var zsw = new Stopwatch();
            var sw = new Stopwatch();
            while (Data.isRun)
            {
                zsw.Start();
                sw.Restart();
                SendMsg($"执行第{i + 1}遍开始");
                //校验3，6，9胜利弹出
                var result = OperationPoint.WhileCheck(5, 300, OperationPoint.OperationType.JS);
                if (result)
                {
                    while (Data.isRun)
                    {
                        //防止结算出来过早没有点击出发成功就结束了，因此要判断到当前的界面没有结算状态才可以
                        result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.JS);
                        if (result)
                        {
                            await Task.Delay(300);
                        }
                        else
                            break;
                    }
                }
                result = OperationPoint.WhileCheck(5, 1000, OperationPoint.OperationType.JJTP_WD, 5);
                if (!result)
                {
                    SendMsg($"都打过了，等待刷新");
                    result = OperationPoint.WhileCheck(5, 1000, OperationPoint.OperationType.JJTP_SX, 5);
                    if (!result)
                    {

                        SendMsg($"不可以刷新，退出任务");
                        return;
                    }
                    else
                    {
                        continue;
                    }
                }
                result = OperationPoint.WhileCheck(5, 300, OperationPoint.OperationType.JJTP_JG, 10);
                if (!result)
                {
                    SendMsg($"找不到进攻按钮，退循环");
                    return;
                }
                result = OperationPoint.WhileCheck(5, 300, OperationPoint.OperationType.JJTP_JBG, 10);
                if (result)
                {
                    SendMsg($"突破卷不足，结束突破");
                    return;
                }
                await Task.Delay(5000);
                //判断是否要手动准备
                result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.ZB, 5);
                await Task.Delay(20000);
                while (Data.isRun)
                {
                    result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.JS, 20);
                    if (!result)
                        result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.JJTP_SB, 20);
                    else
                    {
                        while (Data.isRun)
                        {
                            //防止结算出来过早没有点击出发成功就结束了，因此要判断到当前的界面没有结算状态才可以
                            result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.JS);
                            if (result)
                            {
                                await Task.Delay(300);
                            }
                            else
                                break;
                        }
                        break;
                    }
                    if (!result)
                    {
                        SendMsg($"战斗继续中");
                        await Task.Delay(1000);
                    }
                    else
                    {
                        while (Data.isRun)
                        {
                            //防止结算出来过早没有点击出发成功就结束了，因此要判断到当前的界面没有结算状态才可以
                            result = OperationPoint.ExisitWithCheck(OperationPoint.OperationType.JJTP_SB);
                            if (result)
                            {
                                await Task.Delay(300);
                            }
                            else
                                break;
                        }
                        break;
                    }
                }
                i++;
                sw.Stop();
                SendMsg($"执行第{i}遍结束,总任务耗时{zsw.Elapsed.TotalSeconds}，单次耗时{sw.Elapsed.TotalSeconds}");
                await Task.Delay(1000);
            }
        }

    }
}